var db = wx.cloud.database()
var activityList = []
var bgList = []
for ( let i = 0; i < 9; i++ ) {
    bgList.push({
        image: "../../images/catan.png",
        name: "卡坦岛",
        selected: false
    })
}

const customCallout1 = {
  id: 2,
  latitude: 23.097994,
  longitude: 113.323520,
  iconPath: '/image/location.png',
  customCallout: {
    anchorY: 0,
    anchorX: 0,
    display: 'ALWAYS'
  },
}


Page({

    /**
     * 页面的初始数据
     */
    data: {
        activityList,
        customCallout1: customCallout1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                active: 2
            })
        }
        this.getActivity()

        let that = this
        wx.getFuzzyLocation({
          success: function(res) {
              console.log(res);
              that.setData({
                  longitude: res.longitude,
                  latitude: res.latitude
              })
          }
            
        })

        console.log(activityList);
        
          
    },
    searchBarOnFocus(e) {
        this.setData({
            searchBarOnFocus: true
        })
    },
    searchBarOnBlur(e) {
        this.setData({
            searchBarOnFocus: false
        })
    },
    gotoCreateActivity(e) {
        wx.navigateTo({
          url: '../CreateActivity/CreateActivity',
        })
    
    },
    gotoJoinActivity(e) {
      console.log(e);
        let id = e.currentTarget.dataset.id
        console.log(id);
        wx.navigateTo({
          url: '../JoinActivity/JoinActivity',
          success: function(res) {
              res.eventChannel.emit('acceptDataFromOpenerPage',{
                id: id
              })
          },
          fail: function(res) {
              console.log(res);
          }
        })
    },
    getActivity(e) {
        let that = this
        activityList = []
        db.collection("activityDB").orderBy('createTime','desc').get({
            success: function(res) {
                console.log(res.data);
                let data = res.data
                let markers = []
                for ( let i in data ) {
                    let activity = {}
                    activity.avatar = data[i].avatar
                    activity.activity = data[i].activity
                    activity.id = data[i]._id
                    activity.location = data[i].location
                    activity.image = data[i].image
                    activity.boardgame = data[i].boardgame
                    let beginTime = new Date(data[i].beginTime)
                    activity.time = beginTime.getFullYear() + "-" + (beginTime.getMonth()+1) + "-" + beginTime.getDate()
                    activity.cost = data[i].cost
                    console.log(activity);
                    activityList.push(activity)
                    let marker = {
                      id: Number(i),
                      longitude: data[i].longitude, 
                      latitude: data[i].latitude, 
                      height: 30, 
                      width: 30
                    }
                    markers.push(marker)
                }
                console.log(markers);
                that.setData({
                    activityList,
                    markers
                })
            }
        })
    },
    ll(e){
      let id = this.data.activityList[e.markerId].id
      console.log(id);
      wx.navigateTo({
        url: '../JoinActivity/JoinActivity',
        success: function(res) {
            res.eventChannel.emit('acceptDataFromOpenerPage',{
              id: id
            })
        },
        fail: function(res) {
            console.log(res);
        }
      })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.getActivity()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})